package com.systemic.service.impl;

import com.systemic.mapper.GroupLogMapper;
import com.systemic.mapper.GroupMindfulnessInfoMapper;
import com.systemic.mapper.GroupMindfulnessMapper;
import com.systemic.mapper.MindfulnessMediaMapper;
import com.systemic.pojo.GroupLog;
import com.systemic.pojo.GroupMindfulness;
import com.systemic.pojo.GroupMindfulnessInfo;
import com.systemic.pojo.MindfulnessMedia;
import com.systemic.service.GroupMindfulnessInfoService;
import com.systemic.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GroupMindfulnessInfoServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 11:02
 * @Version:
 **/
@Service
public class GroupMindfulnessInfoServiceImpl implements GroupMindfulnessInfoService {

    @Resource
    private GroupMindfulnessInfoMapper groupMindfulnessInfoMapper;

    @Resource
    private GroupMindfulnessMapper groupMindfulnessMapper;

    @Resource
    private MindfulnessMediaMapper mindfulnessMediaMapper;

    @Resource
    private GroupLogMapper groupLogMapper;


    /**
     * 发起活动
     *
     * @param groupMindfulnessInfo
     * @return
     */
    @Override
    @Transactional
    public int addMindfulness(GroupMindfulnessInfo groupMindfulnessInfo) {
        int row = 0;
        row = groupMindfulnessInfoMapper.insert(groupMindfulnessInfo)+row;
        if (row!=0){
            Integer gmId = groupMindfulnessInfoMapper.getLastInsertId(); //获取正念活动自增的主键
            // 获取正念id数组，用来新增正念资源关系
            List<Integer> medias = groupMindfulnessInfo.getMedias();
            if (!medias.isEmpty()){
                for (Integer mediaId:medias) {
                    GroupMindfulness groupMindfulness = new GroupMindfulness();
                    groupMindfulness.setGmId(gmId);
                    groupMindfulness.setMediaId(mediaId);
                    groupMindfulnessMapper.insert(groupMindfulness);
                }
            }
        }
        return row;
    }

    /**
     * 查看活动列表
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<GroupMindfulnessInfo> mindfulnessList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        Integer groupId = pageVo.getId();
        return groupMindfulnessInfoMapper.selectListByGid(groupId,offset,pageSize);
    }

    /**
     * 查看活动信息
     *
     * @param id
     * @return
     */
    @Override
    public GroupMindfulnessInfo mindfulnessInfo(Integer id) {
        GroupMindfulnessInfo groupMindfulnessInfo = new GroupMindfulnessInfo();
        groupMindfulnessInfo.setId(id);
        GroupMindfulnessInfo res = groupMindfulnessInfoMapper.selectONE(groupMindfulnessInfo);
        if (res!=null){
            Integer gmId = res.getId(); //获取正念活动的主键
            GroupMindfulness groupMindfulness = new GroupMindfulness();
            groupMindfulness.setGmId(gmId);   // 写入主键，用于查询
            List<GroupMindfulness> mindfulnessList = groupMindfulnessMapper.select(groupMindfulness);  // 查询当前活动关联了哪些正念资源
            if (!mindfulnessList.isEmpty()){
                List<MindfulnessMedia> mediaList = new ArrayList<>();
                for (GroupMindfulness mindfulness:mindfulnessList) {
                    MindfulnessMedia media = mindfulnessMediaMapper.selectById(mindfulness.getMediaId());  // 根据正念id查询正念资源信息
                    mediaList.add(media);
                }
                res.setMediaList(mediaList);
            }

        }
        return res;
    }

    /**
     * 修改活动
     * @param groupMindfulnessInfo
     * @return
     */
    @Override
    @Transactional
    public int mindfulnessInfoUpdate(GroupMindfulnessInfo groupMindfulnessInfo) {
        int row = 0;
        Integer gmId = groupMindfulnessInfo.getId();
        // 删除活动关联的正念
        GroupMindfulness groupMindfulness = new GroupMindfulness();
        groupMindfulness.setGmId(gmId);
        row = groupMindfulnessMapper.delete(groupMindfulness)+row;

        // 获取正念id数组，用来新增正念资源关系
        List<Integer> medias = groupMindfulnessInfo.getMedias();
        if (!medias.isEmpty()){
            for (Integer mediaId:medias) {
                GroupMindfulness mindfulness = new GroupMindfulness();
                mindfulness.setGmId(gmId);
                mindfulness.setMediaId(mediaId);
                groupMindfulnessMapper.insert(mindfulness);
            }
        }
        // 执行更新操作
        row = groupMindfulnessInfoMapper.update(groupMindfulnessInfo)+row;
        return row;
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int mindfulnessDelete(Integer id) {
        int row = 0;
        GroupMindfulnessInfo groupMindfulnessInfo = new GroupMindfulnessInfo();
        groupMindfulnessInfo.setId(id);
        // 删除活动关联的正念
        GroupMindfulness groupMindfulness = new GroupMindfulness();
        groupMindfulness.setGmId(id);
        row = groupMindfulnessMapper.delete(groupMindfulness)+row;
        // 删除活动打卡记录
        GroupLog groupLog = new GroupLog();
        groupLog.setGmId(id);
        groupLogMapper.delete(groupLog);
        row = groupMindfulnessInfoMapper.delete(groupMindfulnessInfo)+row;
        return row;
    }
}
